package com.yangwudi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yangwudi.exception.ClazzHasStudentsException;
import com.yangwudi.mapper.ClazzMapper;
import com.yangwudi.mapper.StudentMapper;
import com.yangwudi.pojo.Clazz;
import com.yangwudi.pojo.ClazzQueryParam;
import com.yangwudi.pojo.PageResult;
import com.yangwudi.service.ClazzService;
import com.yangwudi.utils.ClazzStatusUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper studentMapper;


    @Override
    public List<Clazz> findAll() {
        List<Clazz> findList = clazzMapper.findAll();
//        //封装 status 状态
//        LocalDate now = LocalDate.now(); // 获取当前日期
//        for (Clazz clazz : findList) {
//            if (now.isAfter(clazz.getEndDate())) {
//                clazz.setStatus("已结课");
//            } else if (now.isBefore(clazz.getBeginDate())) {
//                clazz.setStatus("未开班");
//            } else {
//                clazz.setStatus("在读中");
//            }
//        }

        //调用状态封装函数
        for (Clazz clazz : findList) {
            clazz.setStatus(ClazzStatusUtils.computeStatus(clazz));
        }
        return findList;
    }


    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //1. 设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());

        //2. 执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);

//        //3. 封装 status 状态
//        LocalDate currentDate = LocalDate.now(); // 获取当前日期
//        for (Clazz clazz : clazzList) {
//            if (currentDate.isAfter(clazz.getEndDate())) {
//                clazz.setStatus("已结课");
//            } else if (currentDate.isBefore(clazz.getBeginDate())) {
//                clazz.setStatus("未开班");
//            } else {
//                clazz.setStatus("在读中");
//            }
//        }


        //调用状态封装函数
        for (Clazz clazz : clazzList) {
            clazz.setStatus(ClazzStatusUtils.computeStatus(clazz));
        }
        //4. 解析查询结果，并封装
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(p.getTotal(),p.getResult());
    }


    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }


    @Override
    public void deleteById(Integer id) {
        int studentCount = studentMapper.countStudentByClazzId(id);
        if( studentCount > 0 ) {
            throw new ClazzHasStudentsException("对不起, 该班级下有学生, 不能直接删除");
        }
        clazzMapper.deleteById(id);
    }


    @Override
    public Clazz getById(Integer id) {
        return clazzMapper.getById(id);
    }


    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }
}
